# cython: language_level=3
import datetime
import decimal
import json
import time
import math
import numpy


def get_timestamp():
    return time.time() * 1000


def today_date_list(date_str, date_limit, date_format='%Y-%m-%d'):
    date = str_to_datetime(date_str)
    result_list = [datetime_to_str(date - datetime.timedelta(days=i), date_format) for i in range(date_limit)]
    start_day = datetime_to_str(str_to_datetime('2021-02-22'),date_format)  # 从这天开始有数据，之后的都不用管了
    result_list = list(filter(lambda x: str_datetime_compare(x, start_day,date_format) >= 0, result_list))
    return result_list


def get_today_date_str(offset: int):
    """
    获取当前时间的日期时间
    :param offset:
    :return:
    """
    return datetime_to_str(datetime.datetime.now() + datetime.timedelta(days=offset))


def date_str_is_today(date_str):
    return datetime_to_str(datetime.datetime.now()) == date_str


def date_str_is_yesterday(date_str):
    return datetime_to_str(datetime.datetime.now() - datetime.timedelta(days=1)) == date_str


def str_to_datetime(date_str, date_format='%Y-%m-%d'):
    return datetime.datetime.strptime(date_str, date_format)


def datetime_to_str(date, date_format='%Y-%m-%d'):
    return date.strftime(date_format)

def key_underline_to_hump(key:str):
    next_upper = False
    result = '';
    for i in list(key.lower()):
        if i == '_':
            next_upper = True
            continue
        if next_upper:
            next_upper = False
            i = i.upper()
        result += i
    return result
    return key


def str_datetime_compare(time1: str, time2: str, date_format='%Y-%m-%d'):
    """
    时间对比
    :param time1:
    :param time2:
    :param date_format:
    :return: 0 , 1 , -1
    """
    datetime1 = str_to_datetime(time1, date_format)
    datetime2 = str_to_datetime(time2, date_format)
    if datetime1 > datetime2:
        return 1
    elif datetime1 == datetime2:
        return 0
    elif datetime1 < datetime2:
        return -1
    return -999999


class DateEncoder(json.JSONEncoder):
    def default(self, obj):
        # print(type(obj))
        if isinstance(obj, datetime.datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, datetime.date):
            return obj.strftime("%Y-%m-%d %H:%M:%S")
        elif isinstance(obj, decimal.Decimal):
            return float(obj)
        elif isinstance(obj, numpy.long):
            return int(obj)
        elif isinstance(obj, numpy.int64):
            return int(obj)
        elif isinstance(obj, numpy.float):
            return float(obj)
        elif isinstance(obj, numpy.ndarray):
            return obj.tolist()
        else:
            return json.JSONEncoder.default(self, obj)


def truncate_float(number, digits) -> float:
    stepper = 10.0 ** digits
    return math.trunc(stepper * number) / stepper


if __name__ == '__main__':
    # print(today_date_list('2021-02-04', 60))
    # print(date_str_is_today('2021-02-04'))
    # print(date_str_is_today('2021-02-05'))
    # print(date_str_is_today('2021-02-03'))
    # print(str_to_datetime('2020-10-04'))
    # print(datetime_to_str(datetime.datetime.now()))
    #
    # print(date_str_is_today('2021-02-10'))
    # print(date_str_is_yesterday('2021-02-09'))

    print(str_datetime_compare('2021-02-21', '2021-02-22'))
    print(today_date_list('2021-05-07', 240))

    print(key_underline_to_hump('OCR_CARD_NO'))
